''' 
Copyright 2013 Robert Edger

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
'''

import socket
import snarlConfig

def sendSnarlRequest(tipo, params):
    print tipo
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect(snarlConfig.snarlHostAndPort)
    query = '?app-sig='+snarlConfig.snarlAppSig
    for param in params:
        print param
        if params[param] is not None:
            query = query + '&'+param+'='+params[param]
    query = query + "\r\n"
    print 'calling', tipo+query
    s.send('snp://'+tipo+query)
    print s.recv(1024)
    s.close()

#registers this app using the SNP protocol
def registerSnarl():
    sendSnarlRequest('register',{"id":snarlConfig.snarlAppSig,"title":snarlConfig.snarlAppTitle})

#displays a simple Snarl notification
def showNotification(title,text,callback=None,icon=snarlConfig.iconFile,sound=snarlConfig.soundFile):
    sendSnarlRequest('notify',{'title':title,'text':text,'icon':icon,'sound':sound,'callback':callback})
